package com.wtwd.device.model.vo;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wtwd.common.base.model.BaseRespCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zdl
 * @Description
 * @create 2021-06-15 13:35
 */
@ApiModel(value = "接口返回对象", description = "接口返回对象")
public class ResultVO<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 返回消息
     */
    @ApiModelProperty(value = "返回消息")
    private String msg = BaseRespCode.SUCCESS_MSG;

    /**
     * 返回代码
     */
    @ApiModelProperty(value = "返回代码")
    private Integer code = BaseRespCode.SUCCESS_CODE;

    /**
     * 返回数据对象 data
     */
    @ApiModelProperty(value = "返回数据对象")
    private T data;


    public ResultVO() {

    }

    public static <T> ResultVO<T> result(T data, Integer code, String msg) {
        ResultVO<T> r = new ResultVO<T>();
        r.setCode(code);
        r.setMsg(msg);
        r.setData(data);
        return r;
    }

    public static <T> ResultVO<PageDataVO<T>> success(BaseRespCode respCode, IPage pageData) {
        ResultVO<PageDataVO<T>> r = new ResultVO<>();
        r.setCode(respCode.getCode());
        r.setMsg(respCode.getMsg());
        if (pageData == null) {
            return r;
        }
        PageDataVO<T> data = new PageDataVO<>();
        data.setTotal(pageData.getTotal());
        data.setRows(pageData.getRecords());
        r.setData(data);
        return r;
    }

    public static <T> ResultVO<T> success() {
        ResultVO<T> r = new ResultVO<T>();
        return r;
    }

    public static <T> ResultVO<T> success(T data) {
        ResultVO<T> r = new ResultVO<>();
        if (data == null && data instanceof List) {
            ArrayList objects = new ArrayList<T>(0);
            data = ((T) objects);
        }
        r.setData(data);
        return r;
    }


    public static <T> ResultVO<T> success(BaseRespCode respCode, T data) {
        ResultVO<T> r = new ResultVO<>();
        r.setCode(respCode.getCode());
        r.setMsg(respCode.getMsg());
        r.setData(data);
        return r;
    }

    public static <T> ResultVO<T> success(BaseRespCode respCode) {
        ResultVO<T> r = new ResultVO<>();
        r.setCode(respCode.getCode());
        r.setMsg(respCode.getMsg());
        return r;
    }


    public static ResultVO<Object> error(int code, String msg) {
        ResultVO<Object> r = new ResultVO<Object>();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }

    public static <T> ResultVO<T> error(BaseRespCode respCode) {
        ResultVO<T> r = new ResultVO<>();
        r.setCode(respCode.getCode());
        r.setMsg(respCode.getMsg());
        return r;
    }

    public static <T> ResultVO<T> error() {
        ResultVO r = new ResultVO<Object>();
        r.setCode(BaseRespCode.ERROR_CODE);
        r.setMsg(BaseRespCode.ERROR_MSG);
        return r;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
